package account

import (
	"account/models"
	"account/server"
	"fmt"
	"net/http"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

// @Summary 获取所有删除或者停用账户信息
// @Description get account black info
// @Accept  json
// @Produce json
// @Success 200 {string} string "success"
// @Router /api/v1/account/black [get]
func AccountBlackAll(ctx *gin.Context) {
	var response *server.Response

	result, err := server.GetAccountBlackAll()
	if err != nil {
		message := fmt.Sprintf("没有获取导数据 : %s", err.Error())
		response = server.NewResponse(http.StatusNoContent, message, result)
		ctx.JSON(http.StatusNoContent, response)
		return

	}
	message := "success"
	response = server.NewResponse(http.StatusOK, message, result)
	ctx.JSON(http.StatusOK, response)

}

// @Summary 获取所有账户信息
// @Description get account info
// @Accept  json
// @Produce json
// @Success 200 {string} string "success"
// @Router /api/v1/account [get]
func AccountAllInfos(ctx *gin.Context) {

	var response *server.Response

	result, err := server.AcquireAccountCacheInfos()
	if err != nil {
		message := fmt.Sprintf("没有获取导数据 : %s", err.Error())
		response = server.NewResponse(http.StatusNoContent, message, result)
		ctx.JSON(http.StatusNoContent, response)
		return

	}
	message := "success"
	response = server.NewResponse(http.StatusOK, message, result)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 获取某一个账户信息
// @Description get account one
// @Accept  json
// @Produce json
// @Success 200 {string} string "success"
// @Router /api/v1/account/:id [get]
func AccountFetchOne(ctx *gin.Context) {

	var response *server.Response

	accountId := ctx.Param("id")
	if strings.Compare(accountId, "") == 0 {
		message := "缺少必传参数"
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	id, err := strconv.Atoi(accountId)
	if err != nil {
		message := fmt.Sprintf("数据格式转换失败 : %s", err.Error())
		response = server.NewResponse(http.StatusGone, message, []interface{}{})
		ctx.JSON(http.StatusGone, response)
		return
	}

	result, err := server.FetchAccountOne(id)
	if err != nil {
		message := fmt.Sprintf("没有获取导数据 : %s", err.Error())
		response = server.NewResponse(http.StatusNoContent, message, result)
		ctx.JSON(http.StatusNoContent, response)
		return

	}
	message := "success"
	response = server.NewResponse(http.StatusOK, message, result)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 新增账户信息
// @Description get account info
// @Accept  json
// @Produce json
// @Param account body models.Account true "Account"
// @Success 200 {string} string "success"
// @Router /api/v1/account/add [post]
func AccountAdd(ctx *gin.Context) {
	var response *server.Response
	var account models.Account
	var err error

	account.Active, err = strconv.ParseBool(ctx.DefaultQuery("active", "1"))
	if err != nil {
		message := fmt.Sprintf("请求参数异常: %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	account.Deleted, err = strconv.ParseBool(ctx.DefaultQuery("deleted", "0"))
	if err != nil {
		message := fmt.Sprintf("请求参数异常: %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	if err := ctx.Bind(&account); err != nil {
		message := fmt.Sprintf("请求参数异常: %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	result, err := server.AddAccountInfo(account)
	if err != nil {
		message := fmt.Sprintf("添加信息失败: %s", err.Error())
		response = server.NewResponse(http.StatusExpectationFailed, message, []interface{}{})
		ctx.JSON(http.StatusExpectationFailed, response)
		return
	}

	message := "success"
	response = server.NewResponse(http.StatusOK, message, result)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 更新账户信息
// @Description get account info
// @Accept  json
// @Produce json
// @Param account body models.Account true "Account"
// @Param id path int true "Account Id"
// @Success 200 {string} string "success"
// @Router /api/v1/account/update/:id [post]
func AccountUpdateInfo(ctx *gin.Context) {
	var response *server.Response
	var account models.Account

	accountId := ctx.Param("id")
	if strings.Compare(accountId, "") == 0 {
		message := "缺少必传参数"
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	id, err := strconv.Atoi(accountId)
	if err != nil {
		message := fmt.Sprintf("数据格式转换失败 : %s", err.Error())
		response = server.NewResponse(http.StatusGone, message, []interface{}{})
		ctx.JSON(http.StatusGone, response)
		return
	}

	if err := ctx.Bind(&account); err != nil {
		message := fmt.Sprintf("请求参数异常: %s", err.Error())
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return

	}

	if err := server.UpdateAccountInfo(id, account); err != nil {
		message := fmt.Sprintf("更新信息失败 : %s", err.Error())
		response = server.NewResponse(http.StatusExpectationFailed, message, []interface{}{})
		ctx.JSON(http.StatusExpectationFailed, response)
		return

	}

	message := "success"
	response = server.NewResponse(http.StatusOK, message, account)
	ctx.JSON(http.StatusOK, response)
}

// @Summary 删除市场信息
// @Description get marekt info
// @Accept  json
// @Produce json
// @Param id path int true "Account Id"
// @Success 200 {string} string "success"
// @Router /api/v1/market/active/:id [post]
func AccountActive(ctx *gin.Context) {
	var response *server.Response

	accountId := ctx.Param("id")
	if strings.Compare(accountId, "") == 0 {
		message := "缺少必传参数"
		response = server.NewResponse(http.StatusBadRequest, message, []interface{}{})
		ctx.JSON(http.StatusBadRequest, response)
		return
	}

	id, _ := strconv.Atoi(accountId)

	if err := server.ActiveAccountInfo(id); err != nil {
		message := fmt.Sprintf("更新信息失败: %s", err.Error())
		response = server.NewResponse(http.StatusExpectationFailed, message, []interface{}{})
		ctx.JSON(http.StatusExpectationFailed, response)
		return

	}

	message := "success"
	response = server.NewResponse(http.StatusOK, message, []interface{}{})
	ctx.JSON(http.StatusOK, response)
}
